package com.jsbs.iam.user.controller;

import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.common.core.utils.Json;
import com.jsbs.iam.common.core.utils.StringUtils;
import com.jsbs.iam.user.constant.Constants;
import com.jsbs.iam.user.dto.*;
import com.jsbs.iam.user.entity.IamApplySystem;
import com.jsbs.iam.user.service.MenuManagerService;
import com.jsbs.iam.user.vo.ButtonInfoVo;
import com.jsbs.iam.user.vo.MenuManagerInfoVo;
import com.jsbs.iam.user.vo.MenuThreeVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: iam
 * @BelongsPackage: com.jsbs.iam.user.controller
 * @Author: WangJiYuan
 * @Description: 应用中心  菜单配置
 * @Version: 2023-01-04  18:38
 */
@Slf4j
@RestController
@RequestMapping("/iam/user/menu")
public class MenuManagerController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(MenuManagerController.class);

    @Autowired
    private MenuManagerService menuManagerService;

    private static final String ADMIN_USER = "admin";

    /**
     * 获取菜单树(各个应用)
     * @param httpServletRequest
     * @param menuManagerSelectDto
     * @return
     */
    @PostMapping("/getMenuThreeInfo")
    public Result getMenuThreeInfo(HttpServletRequest httpServletRequest, @RequestBody MenuManagerSelectDto menuManagerSelectDto) {
        logger.info("Route=>MenuManagerController method=>getMenuThreeInfo 获取菜单树入参：{}", Json.toJsonString(menuManagerSelectDto));
        try {
            // todo  wjy  临时获取companyCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>MenuManagerController method=>getMenuThreeInfo 获取菜单树入参，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            menuManagerSelectDto.setCompanyCode(companyCode);

            if (StringUtils.isEmpty(menuManagerSelectDto.getApplyCode())) {
                logger.warn("Route=>MenuManagerController method=>getMenuThreeInfo 查询组织树结构应用编码为空");
                return Result.failWithMsg("应用编码为空");
            }

            // 获取菜单树信息
            List<MenuThreeVo> threeInfos = menuManagerService.getMenuThreeInfo(menuManagerSelectDto);
            return new Result(Result.SUCCESS, "获取菜单树成功", threeInfos);
        } catch (Exception e) {
            logger.error("Route==>MenuManagerController method=>getMenuThreeInfo 获取菜单树异常，入参：{}，异常：{}", Json.toJsonString(menuManagerSelectDto), e);
            return handleException(e);
        }
    }

    /**
     * 获取所有应用数据
     * @return
     */
    @PostMapping("/getApplyData")
    public Result getApplyData(HttpServletRequest httpServletRequest) {
        logger.info("Route=>MenuManagerController method=>getApplyData 获取所有应用数据：{}", "");
        try {
            //参数
            Map<String, String> params = new HashMap<String, String>();
            // todo  wjy  临时获取companyCode方法
            String companyCode = (String) httpServletRequest.getSession().getAttribute(Constants.COMPANY_CODE);
            String userCode = (String) httpServletRequest.getSession().getAttribute(Constants.USER_CODE);
            String adminRoleCode = httpServletRequest.getHeader(Constants.ADMIN_ROLE_CODE);
            companyCode = "000001";
            userCode = "admin";
            adminRoleCode = "JS0030";
            params.put("companyCode", companyCode);
            params.put("userCode", userCode);
            params.put("adminRoleCode", adminRoleCode);
            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>MenuManagerController method=>getApplyData 获取所有应用数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>MenuManagerController method=>getApplyData 获取所有应用数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            if (!ADMIN_USER.equals(userCode) && StringUtils.isEmpty(adminRoleCode)) {
                logger.warn("Route=>MenuManagerController method=>getApplyData 获取所有应用数据，当前登录人角色信息异常");
                return Result.failWithMsg("登录人角色信息异常");
            }
            //获取所有应用数据
           List<IamApplySystem> applyData = menuManagerService.getApplyData(params);

            return Result.success("获取所有应用数据成功", applyData);
        } catch (Exception e) {
            logger.error("Route==>MenuManagerController method=>getApplyData 获取所有应用数据，入参：{}，异常：{}","", e);
            return handleException(e);
        }
    }


    /**
     * 新增菜单
     * @param menuAddDto
     * @return
     */
    @PostMapping("/addMenuInfo")
    public Result addMenuInfo(HttpServletRequest httpServletRequest, @RequestBody MenuManagerAddDto menuAddDto) {
        logger.info("Route=>MenuManagerController method=>addMenuInfo 新增菜单：{}", Json.toJsonString(menuAddDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>MenuManagerController method=>addMenuInfo 新增菜单，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>MenuManagerController method=>addMenuInfo 新增菜单，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>MenuManagerController method=>addMenuInfo 新增菜单，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            menuAddDto.setCompanyCode(companyCode);
            menuAddDto.setCreatePerson(userName);

            if (StringUtils.isEmpty(menuAddDto.getMenuName())) {
                logger.info("新增菜单失败,参数非法{}", menuAddDto.getMenuName());
                return Result.fail(Result.FAIL, "菜单名称不可为空");
            } else {
                //校验同级别名称唯一
                int count = menuManagerService.verifyMenuName(menuAddDto.getMenuSuperiorCode(), menuAddDto.getMenuName(), companyCode);
                if (count > 0) {
                    logger.info("新增菜单失败,参数非法{}", menuAddDto.getMenuName());
                    return Result.fail(Result.FAIL, "同一级别名称不可重复");
                }
            }
            /*if (StringUtils.isEmpty(menuAddDto.getMenuUrl())) {
                logger.info("新增菜单失败,参数非法{}", menuAddDto.getMenuUrl());
                return Result.fail(Result.FAIL, "菜单链接不可为空");
            }*/
            if (menuAddDto.getMenuStatus() == null) {
                logger.info("新增菜单失败,参数非法{}", menuAddDto.getMenuStatus());
                return Result.fail(Result.FAIL, "菜单状态不可为空");
            }
            if (menuAddDto.getMenuLevel() == null) {
                logger.info("新增菜单失败,参数非法{}", menuAddDto.getMenuLevel());
                return Result.fail(Result.FAIL, "菜单层级不可为空");
            }
            //新增菜单
            menuManagerService.addMenuInfo(menuAddDto);
            return Result.successWithMsg("新增菜单成功");
        } catch (Exception e) {
            logger.error("Route==>MenuManagerController method=>addMenuInfo 新增菜单异常，入参：{}，异常：{}", Json.toJsonString(menuAddDto), e);
            return handleException(e);
        }
    }

    /**
     * 删除菜单数据
     * @param menuDeleteDto
     * @return
     */
    @PostMapping("/deleteMenuInfo")
    public Result deleteMenuInfo(HttpServletRequest httpServletRequest, @RequestBody MenuManagerDeleteDto menuDeleteDto) {
        logger.info("Route=>MenuManagerController method=>deleteMenuInfo 删除菜单数据：{}", Json.toJsonString(menuDeleteDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>MenuManagerController method=>deleteMenuInfo 删除菜单数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>MenuManagerController method=>deleteMenuInfo 删除菜单数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>MenuManagerController method=>deleteMenuInfo 删除菜单数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            menuDeleteDto.setCompanyCode(companyCode);
            menuDeleteDto.setUpdatePerson(userName);

            if (StringUtils.isEmpty(menuDeleteDto.getMenuCode())) {
                logger.info("删除菜单数据失败,参数非法{}", menuDeleteDto.getMenuCode());
                return Result.failWithMsg("菜单编码不可为空");
            }
            if (StringUtils.isEmpty(menuDeleteDto.getApplyCode())) {
                logger.info("删除菜单数据失败,参数非法{}", menuDeleteDto.getApplyCode());
                return Result.failWithMsg("应用编码不可为空");
            }
            //删除菜单数据
            menuManagerService.deleteMenuInfo(menuDeleteDto);
            return Result.successWithMsg("删除菜单数据成功");
        } catch (Exception e) {
            logger.error("Route==>MenuManagerController method=>deleteMenuInfo 删除菜单数据异常，入参：{}，异常：{}", Json.toJsonString(menuDeleteDto), e);
            return handleException(e);
        }
    }

    /**
     * 获取菜单详细数据
     * @param menuCode
     * @return
     */
    @PostMapping("/selectMenuData")
    public Result selectMenuData(HttpServletRequest httpServletRequest, @RequestParam("menuCode") String menuCode) {
        logger.info("Route=>MenuManagerController method=>selectMenuData 获取菜单详细数据：{}", menuCode);
        try {
            // todo  wjy  临时获取companyCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                log.warn("Route=>MenuManagerController method=>selectMenuData 获取菜单详细数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            if (StringUtils.isEmpty(menuCode)) {
                logger.info("获取菜单详细数据失败,参数非法{}", menuCode);
                return Result.fail(Result.FAIL, "菜单编码不可为空");
            }
            //获取菜单详细数据
            MenuManagerInfoVo menuManagerInfoVo = menuManagerService.selectMenuData(menuCode,companyCode);

            return Result.success("根据组织编码查询组织数据成功", menuManagerInfoVo);
        } catch (Exception e) {
            logger.error("Route==>MenuManagerController method=>selectMenuData 获取菜单详细数据异常，入参：{}，异常：{}", menuCode, e);
            return handleException(e);
        }
    }

    /**
     * 修改菜单数据
     * @param menuAddDto
     * @return
     */
    @PostMapping("/updateMenuInfo")
    public Result updateMenuInfo(HttpServletRequest httpServletRequest, @RequestBody MenuManagerAddDto menuAddDto) {
        logger.info("Route=>MenuManagerController method=>updateMenuInfo 修改菜单数据：{}", Json.toJsonString(menuAddDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>MenuManagerController method=>updateMenuInfo 修改菜单数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>MenuManagerController method=>updateMenuInfo 修改菜单数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>MenuManagerController method=>updateMenuInfo 修改菜单数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            menuAddDto.setCompanyCode(companyCode);
            menuAddDto.setCreatePerson(userName);

            if (StringUtils.isEmpty(menuAddDto.getApplyCode())) {
                logger.info("修改菜单数据失败,参数非法{}", menuAddDto.getApplyCode());
                return Result.fail(Result.FAIL, "应用编码不可为空");
            }
            if (StringUtils.isEmpty(menuAddDto.getMenuCode())) {
                logger.info("修改菜单数据失败,参数非法{}", menuAddDto.getMenuCode());
                return Result.fail(Result.FAIL, "菜单编码不可为空");
            }
            if (StringUtils.isEmpty(menuAddDto.getMenuName())) {
                logger.info("修改菜单数据失败,参数非法{}", menuAddDto.getMenuName());
                return Result.fail(Result.FAIL, "菜单名称不可为空");
            } else {
                //校验是否修改菜单名称
                String menuName = menuManagerService.getMenuName(menuAddDto.getMenuCode(),companyCode);
                if (!menuAddDto.getMenuName().equals(menuName)) {
                    //校验同级别名称唯一
                    int count = menuManagerService.verifyMenuName(menuAddDto.getMenuSuperiorCode(), menuAddDto.getMenuName(),companyCode);
                    if (count > 0) {
                        logger.info("修改菜单数据失败,参数非法{}", menuAddDto.getMenuName());
                        return Result.fail(Result.FAIL, "同一级别名称不可重复");
                    }
                }
            }
            if (StringUtils.isEmpty(menuAddDto.getMenuUrl())) {
                logger.info("修改菜单数据失败,参数非法{}", menuAddDto.getMenuUrl());
                return Result.fail(Result.FAIL, "菜单链接不可为空");
            }
            if (menuAddDto.getMenuStatus() == null) {
                logger.info("修改菜单数据失败,参数非法{}", menuAddDto.getMenuStatus());
                return Result.fail(Result.FAIL, "菜单状态不可为空");
            }
            //修改菜单数据
            menuManagerService.updateMenuInfo(menuAddDto);
            return Result.successWithMsg("修改菜单数据成功");
        } catch (Exception e) {
            logger.error("Route==>MenuManagerController method=>updateMenuInfo 修改菜单数据异常，入参：{}，异常：{}", Json.toJsonString(menuAddDto), e);
            return handleException(e);
        }
    }

    /**
     * 菜单移动
     * @param menuAddDto
     * @return
     */
    @PostMapping("/moveMenu")
    public Result moveMenu(HttpServletRequest httpServletRequest, @RequestBody MenuManagerAddDto menuAddDto) {
        logger.info("Route=>MenuManagerController method=>moveMenu 菜单移动：{}", Json.toJsonString(menuAddDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>MenuManagerController method=>moveMenu 菜单移动，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>MenuManagerController method=>moveMenu 菜单移动，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>MenuManagerController method=>moveMenu 菜单移动，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            menuAddDto.setCompanyCode(companyCode);
            menuAddDto.setCreatePerson(userName);

            //将要移动的层级
            if (StringUtils.isEmpty(menuAddDto.getMenuCode())) {
                logger.info("菜单移动失败,参数非法{}", menuAddDto.getMenuCode());
                return Result.fail(Result.FAIL, "将要移动的菜单编码不可为空");
            }
            //目标层级
            if (StringUtils.isEmpty(menuAddDto.getMenuSuperiorCode())) {
                logger.info("菜单移动失败,参数非法{}", menuAddDto.getMenuSuperiorCode());
                return Result.fail(Result.FAIL, "目标层级菜单编码不可为空");
            }
            if (menuAddDto.getMenuLevel() == null) {
                logger.info("菜单移动失败,参数非法{}", menuAddDto.getMenuLevel());
                return new Result(Result.FAIL, "菜单层级不可为空");
            } else {
                //根据菜单编码获取对应层级
                int level = menuManagerService.verifyMenuLevel(menuAddDto.getMenuCode());
                //校验菜单移动是否为同级移动
                if (level != (menuAddDto.getMenuLevel() + 1)) {
                    logger.info("菜单移动失败,参数非法{}", menuAddDto.getMenuLevel());
                    return new Result(Result.FAIL, "只能平级移动");
                }
            }
            //菜单移动
            menuManagerService.moveMenu(menuAddDto);
            logger.info("Route=>MenuManagerController method=>moveMenu 菜单移动>>成功：出参：{}", "");
            return Result.successWithMsg("菜单移动成功");
        } catch (Exception e) {
            logger.error("Route==>MenuManagerController method=>moveMenu 菜单移动>>异常，入参：{}，异常：{}", Json.toJsonString(menuAddDto), e);
            return handleException(e);
        }
    }

    /**
     * 通过菜单编码获取按钮数据
     * @param menuCode
     * @return
     */
    @PostMapping("/selectButtonData")
    public Result selectButtonData(HttpServletRequest httpServletRequest, @RequestParam("menuCode") String menuCode) {
        logger.info("Route=>MenuManagerController method=>selectButtonData 通过菜单编码获取按钮数据：{}", menuCode);
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";

            if (StringUtils.isEmpty(companyCode)) {
                log.warn("Route=>MenuManagerController method=>selectButtonData 通过菜单编码获取按钮数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }

            if (StringUtils.isEmpty(menuCode)) {
                logger.info("通过菜单编码获取按钮数据>>失败,参数非法{}", menuCode);
                return Result.fail(Result.FAIL, "菜单编码不可为空");
            }
            //通过菜单编码获取按钮数据
            List<ButtonInfoVo> buttonInfoVo = menuManagerService.selectButtonData(menuCode, companyCode);
            logger.info("Route=>MenuManagerController method=>selectButtonData 通过菜单编码获取按钮数据>>成功：出参：{}", Json.toJsonString(buttonInfoVo));
            return Result.success("通过菜单编码获取按钮数据成功", buttonInfoVo);
        } catch (Exception e) {
            logger.error("Route==>MenuManagerController method=>selectButtonData 通过菜单编码获取按钮数据>>异常，入参：{}，异常：{}", menuCode, e);
            return handleException(e);
        }
    }

    /**
     * 新增按钮数据
     * @param buttonAddDto
     * @return
     */
    @PostMapping("/addButtonInfo")
    public Result addButtonInfo(HttpServletRequest httpServletRequest,@RequestBody ButtonAddDto buttonAddDto) {
        logger.info("Route=>MenuManagerController method=>addButtonInfo 新增按钮数据：{}", Json.toJsonString(buttonAddDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>MenuManagerController method=>addButtonInfo 新增按钮数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>MenuManagerController method=>addButtonInfo 新增按钮数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>MenuManagerController method=>addButtonInfo 新增按钮数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            buttonAddDto.setCompanyCode(companyCode);
            buttonAddDto.setCreatePerson(userName);

            if (StringUtils.isEmpty(buttonAddDto.getMenuCode())) {
                logger.info("新增按钮数据>>失败,参数非法{}", buttonAddDto.getMenuCode());
                return Result.fail(Result.FAIL, "菜单编码不可为空");
            }
            if (StringUtils.isEmpty(buttonAddDto.getButtonName())) {
                logger.info("新增按钮数据>>失败,参数非法{}", buttonAddDto.getButtonName());
                return Result.fail(Result.FAIL, "按钮名称不可为空");
            } else {
                //校验同级别按钮名称唯一
                int count = menuManagerService.verifyButtonName(buttonAddDto.getMenuCode(), buttonAddDto.getButtonName(), companyCode);
                if (count > 0) {
                    logger.info("新增按钮数据>>失败,参数非法{}", buttonAddDto.getButtonName());
                    return Result.fail(Result.FAIL, "同一级别按钮名称不可重复");
                }
            }
            if (buttonAddDto.getButtonSort() == null) {
                logger.info("新增按钮数据>>失败,参数非法{}", buttonAddDto.getButtonSort());
                return Result.fail(Result.FAIL, "按钮排序号不可为空");
            }
            if (buttonAddDto.getButtonStatus() == null) {
                logger.info("新增按钮数据>>失败,参数非法{}", buttonAddDto.getButtonStatus());
                return Result.fail(Result.FAIL, "按钮状态不可为空");
            }
            //新增按钮数据
            menuManagerService.addButtonInfo(buttonAddDto);
            logger.info("Route=>MenuManagerController method=>addButtonInfo 新增按钮数据>>成功：出参：{}", "");
            return Result.successWithMsg("新增按钮数据成功");
        } catch (Exception e) {
            logger.error("Route==>MenuManagerController method=>addButtonInfo 新增按钮数据>>异常，入参：{}，异常：{}", Json.toJsonString(buttonAddDto), e);
            return handleException(e);
        }
    }

    /**
     * 修改按钮数据
     * @param buttonAddDto
     * @return
     */
    @PostMapping("/updateButtonInfo")
    public Result updateButtonInfo(HttpServletRequest httpServletRequest,@RequestBody ButtonAddDto buttonAddDto) {
        logger.info("Route=>MenuManagerController method=>updateButtonInfo 修改按钮数据：{}", Json.toJsonString(buttonAddDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>MenuManagerController method=>addButtonInfo 新增按钮数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>MenuManagerController method=>addButtonInfo 新增按钮数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>MenuManagerController method=>addButtonInfo 新增按钮数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            buttonAddDto.setCompanyCode(companyCode);
            buttonAddDto.setCreatePerson(userName);

            if (StringUtils.isEmpty(buttonAddDto.getButtonCode())) {
                logger.info("修改按钮数据>>失败,参数非法{}", buttonAddDto.getButtonCode());
                return Result.fail(Result.FAIL, "按钮编码不可为空");
            }
            if (StringUtils.isEmpty(buttonAddDto.getButtonName())) {
                logger.info("修改按钮数据>>失败,参数非法{}", buttonAddDto.getButtonName());
                return Result.fail(Result.FAIL, "按钮名称不可为空");
            } else {
                //校验是否修改名称
                String buttonName = menuManagerService.getButtonName(buttonAddDto.getButtonCode(),companyCode);
                if (!buttonAddDto.getButtonName().equals(buttonName)) {
                    //校验同级别按钮名称唯一
                    int count = menuManagerService.verifyButtonName(buttonAddDto.getMenuCode(), buttonAddDto.getButtonName(),companyCode);
                    if (count > 0) {
                        logger.info("修改按钮数据>>失败,参数非法{}", buttonAddDto.getButtonName());
                        return Result.fail(Result.FAIL, "同一级别按钮名称不可重复");
                    }
                }
            }
            if (buttonAddDto.getButtonSort() == null) {
                logger.info("修改按钮数据>>失败,参数非法{}", buttonAddDto.getButtonSort());
                return Result.fail(Result.FAIL, "按钮排序号不可为空");
            }
            if (buttonAddDto.getButtonStatus() == null) {
                logger.info("修改按钮数据>>失败,参数非法{}", buttonAddDto.getButtonStatus());
                return Result.fail(Result.FAIL, "按钮状态不可为空");
            }
            //修改按钮数据
            menuManagerService.updateButtonInfo(buttonAddDto);
            logger.info("Route=>MenuManagerController method=>updateButtonInfo 修改按钮数据>>成功：出参：{}", "");
            return Result.successWithMsg("修改按钮数据成功");
        } catch (Exception e) {
            logger.error("Route==>MenuManagerController method=>updateButtonInfo 修改按钮数据>>异常，入参：{}，异常：{}", Json.toJsonString(buttonAddDto), e);
            return handleException(e);
        }
    }

    /**
     * 批量删除按钮数据
     * @param buttonDeleteDto
     * @return
     */
    @PostMapping("/deleteButtonData")
    public Result deleteButtonData(HttpServletRequest httpServletRequest, @RequestBody ButtonDeleteDto buttonDeleteDto) {
        logger.info("Route=>MenuManagerController method=>deleteButtonData 批量删除按钮数据：{}", Json.toJsonString(buttonDeleteDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>MenuManagerController method=>deleteButtonData 批量删除按钮数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>MenuManagerController method=>deleteButtonData 批量删除按钮数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>MenuManagerController method=>deleteButtonData 批量删除按钮数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            buttonDeleteDto.setCompanyCode(companyCode);
            buttonDeleteDto.setUpdatePerson(userName);

            if (CollectionUtils.isEmpty(buttonDeleteDto.getButtonCodeData())) {
                logger.info("批量删除按钮数据>>失败,参数非法{}", Json.toJsonString(buttonDeleteDto));
                return Result.failWithMsg("请选择删除数据");
            }
            //批量删除按钮数据
            menuManagerService.deleteButtonData(buttonDeleteDto);
            return Result.successWithMsg("批量删除按钮数据成功");
        } catch (Exception e) {
            logger.error("Route==>MenuManagerController method=>deleteButtonData 批量删除按钮数据>>异常，入参：{}，异常：{}", Json.toJsonString(buttonDeleteDto), e);
            return handleException(e);
        }
    }

    /**
     * 修改按钮状态
     * @param buttonStatusUpdateDto
     * @return
     */
    @PostMapping("/updateButtonStatus")
    public Result updateButtonStatus(HttpServletRequest httpServletRequest, @RequestBody ButtonStatusUpdateDto buttonStatusUpdateDto) {
        logger.info("Route=>MenuManagerController method=>updateButtonStatus 修改按钮状态：{}", buttonStatusUpdateDto);
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>MenuManagerController method=>updateButtonStatus 修改按钮状态，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>MenuManagerController method=>updateButtonStatus 修改按钮状态，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>MenuManagerController method=>updateButtonStatus 修改按钮状态，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            buttonStatusUpdateDto.setCompanyCode(companyCode);
            buttonStatusUpdateDto.setUpdatePerson(userName);

            if (StringUtils.isEmpty(buttonStatusUpdateDto.getButtonCode())) {
                logger.info("修改按钮状态>>失败,参数非法{}", buttonStatusUpdateDto.getButtonCode());
                return Result.fail(Result.FAIL, "按钮编码不可为空");
            }
            if (null == buttonStatusUpdateDto.getButtonStatus()) {
                logger.info("修改按钮状态>>失败,参数非法{}", buttonStatusUpdateDto.getButtonStatus());
                return Result.fail(Result.FAIL, "按钮状态不可为空");
            }
            //修改按钮状态
            menuManagerService.updateButtonStatus(buttonStatusUpdateDto);
            logger.info("Route=>MenuManagerController method=>updateButtonStatus 修改按钮状态>>成功：出参：{}", "");
            return Result.successWithMsg("修改按钮状态成功");
        } catch (Exception e) {
            logger.error("Route==>MenuManagerController method=>updateButtonStatus 修改按钮状态>>异常，入参：{}，异常：{}", buttonStatusUpdateDto, e);
            return handleException(e);
        }
    }
}
